from expression import *
from customExceptions import ReturnExcpt
import sys
sys.path.append(sys.path[0] + "/../LLVM")
from llvmhelper import llvmHelper
from llvm import *
from commonRoutines import commonRoutines


class sequence(expression):
	def __init__(self, listex):
		if(listex != []):
			self.last = listex.pop()
		else:
			self.last = None
		self.listex = listex
	def evaluate(self,env):
		for e in self.listex:
			e.evaluate(env)
		if(self.last == None):
			return  
		return self.last.evaluate(env)
	def __str__(self):
		retval = "sequence([\n"
		for exp in self.listex:
			retval += str(exp) + ",\n"
		retval += str(self.last) + "]"
		return retval
	
	def traverse(self, helper):
		for e in self.listex:
		#	print "Traversing in sequence \n" + str(e) + "\n"
			e.traverse(helper)
		#print "Traversing in sequence last \n " + str(self.last) + "\n"
		if(self.last == None):
			return  
		self.last.traverse(helper)
		
	def toLLVM(self, helper):
		helper.descend("varBinding")
		
		for l in self.listex:
			l.toLLVM(helper)
			
		self.last.toLLVM(helper)
		c = commonRoutines(helper)
		
		# return something?
		
		helper.setLabel(helper.getLastLabel())
		helper.ascend(helper.getReturns()[-1]) #what do I do here?
